﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using System.Windows.Resources;

namespace MRadio
{
    public partial class FequenceDislayer : UserControl
    {
        #region -- 绑定属性 --

        private DisplayerModel model;
      

        private float _fequenceValue = 76.00F;
        #endregion

        public FequenceDislayer()
        {           
            // 为初始化变量所必需
            InitializeComponent();
           
           this.Loaded += new RoutedEventHandler(FequenceDislayer_Loaded);           
        }

        void FequenceDislayer_Loaded(object sender, RoutedEventArgs e)
        {
            model = new DisplayerModel(GetImageByNumber(100));
            this.DataContext = model;
        }
        /// <summary>
        /// 获取或设置频率值
        /// </summary>
        public float FequenceValue
        {
            get
            {
                return _fequenceValue;
            }
            set
            {
               // bool ifchange=_fequenceValue == value;
                _fequenceValue = value;
               // if(!ifchange)
                    OnValueChange();
            }
        }

        private void OnValueChange()
        {
            string str = _fequenceValue.ToString("F1");//string.Format("F1", _fequenceValue);
            if (_fequenceValue >= 100)
            {
                bit0.Visibility = Visibility.Visible;
                model.Bit0Source = GetImageByNumber(Int16.Parse(str[0].ToString()));//((int)_fequenceValue) / 100
                //105.8
                model.Bit1Source = GetImageByNumber(Int16.Parse(str[1].ToString()));
                model.Bit2Source = GetImageByNumber(Int16.Parse(str[2].ToString()));
                model.Bit3Source = GetImageByNumber(Int16.Parse(str[4].ToString()));
            }
            else
            {
                bit0.Visibility = Visibility.Collapsed;
                //78.0
                model.Bit1Source = GetImageByNumber(Int16.Parse(str[0].ToString()));
                model.Bit2Source = GetImageByNumber(Int16.Parse(str[1].ToString()));
                model.Bit3Source = GetImageByNumber(Int16.Parse(str[3].ToString()));
            }
        }

        private BitmapImage GetImageByNumber(int number)
        {
            string asmName = System.Reflection.Assembly.GetExecutingAssembly().FullName.Split(',')[0];
            return new BitmapImage(new Uri(string.Format("/{0};component/Resources/Images/Num_{1}.png", asmName, number),
                                                UriKind.Relative));
            //StreamResourceInfo r = Application.GetResourceStream(new Uri(string.Format("Resources/Images/Num_{0}.png", number), UriKind.Relative));
            //BitmapImage bmp = new BitmapImage();
            //bmp.SetSource(r.Stream);
            //return bmp;
        }

    }

    public class DisplayerModel : INotifyPropertyChanged
    {
        private BitmapImage _bit0Source;
        private BitmapImage _bit1Source;
        private BitmapImage _bit2Source;
        private BitmapImage _bit3Source;

        public DisplayerModel(BitmapImage defaultImg)
        {
            _bit0Source = defaultImg;
            _bit1Source = defaultImg;
            _bit2Source = defaultImg;
            _bit3Source = defaultImg;
        }
       
        public BitmapImage Bit0Source 
        {
            get { return _bit0Source; }
            set {
                _bit0Source = value;
                if (value != null)
                {
                    NotifyPropertyChanged("Bit0Source");
                }
            }
        }
        public BitmapImage Bit1Source 
        {
            get { return _bit1Source; }
            set {
                _bit1Source = value;
                if (value != null)
                {
                    NotifyPropertyChanged("Bit1Source");
                }
            }
        }
        public BitmapImage Bit2Source
        {
            get { return _bit2Source; }
            set {
                _bit2Source = value;
                 if (value != null)
                {
                    NotifyPropertyChanged("Bit2Source");
                }
            }
        }
        public BitmapImage Bit3Source 
        {
            get { return _bit3Source; }
            set
            {
                _bit3Source = value;
                 if (value != null)
                {
                    NotifyPropertyChanged("Bit3Source");
                }
            }
        }
        //public BitmapImage Bit4Source { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}